package com.zzf.concurrent.example.design.pattern.count.down;

/**
 * @author GaoFeng2017
 * @date 2019/9/9 17:25
 */
public class CountDownLatch {
    private int taskCount;

    public CountDownLatch(int taskCount) {
        init(taskCount);
    }

    public synchronized void countDown() {
        taskCount--;
        if (taskCount == 0) {
            this.notifyAll();
        }
    }

    public synchronized void await() throws InterruptedException {
        while (taskCount > 0) {
            this.wait();
        }
    }

    private void init(int taskCount) {
        if (taskCount <= 0) {
            throw new IllegalArgumentException();
        }
        this.taskCount = taskCount;
    }
}
